﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Text;

namespace Tablet
{
    public partial class _MoreApps : System.Web.UI.Page
    {
        LocalFunctions FUNC;
        protected string sAppsList = string.Empty;

        private PageType _pageType;
        private int iCurrentPage;
        private int iCatID;
        private int iStartNum;
        private string _SearchTerm;
        private bool isTop;
        private int iPageSize = 5;

        protected void Page_Load(object sender, EventArgs e)
        {
            FUNC = new LocalFunctions();
            GetParameters();
            List<Application> Items = null;
            ArrayList arrCatID ;
            int TotalResults = 0;

            #region Get items by type
            switch (_pageType)
            {
                case PageType.Regular:
                    Items = FUNC.GetAppsByCatID(iCatID, iStartNum, iPageSize, out TotalResults);
                    break;

                case PageType.Reccomended:
                    Items = FUNC.GetRecomendedApps(iStartNum, iPageSize, out TotalResults);
                    break;
                case PageType.Newest:
                    Items = FUNC.GetNewApps(iStartNum, iPageSize, out TotalResults);
                    break;
                case PageType.Popular:
                    Items = FUNC.GetPopularApps(iStartNum, iPageSize, out TotalResults);
                    break;
                case PageType.Search:
                    Items = FUNC.SearchApps(_SearchTerm, iStartNum, iPageSize, out TotalResults);
                    break;
            }
            #endregion

            Boolean isOrange = true;

            if (iCurrentPage == 0)
                sAppsList += "totalItems:" + TotalResults;
            foreach (Application item in Items)
            {
                bool bHaveOrange = false;
                for (int iCount = 0; iCount < item.AppType.Length; iCount++)
                {
                    bHaveOrange = false;
                    if (item.AppType[iCount].ToLower() == "orange")
                    {
                        bHaveOrange = true;
                        break;
                    }
                }
                if (!bHaveOrange && isOrange)
                {
                    sAppsList += "<li class=\"clearfix\">" +
                                "<p class=\"text info\">" +
                                    "אפליקציות המוצגות בהמשך אינן באחריות פרטנר ומצריכות חבילת גלישה. <a href=\"\">לתעריפי" +
                                        "הגלישה לחץ כאן</a>" +
                    "</p><div class=\"cut\">" +
                        "<span class=\"shadow\"></span><span class=\"highlight\"></span>" +
                    "</div><!-- cut --></li>";


                    isOrange = false;
                }
                string sAppName = item.name;
                string sPrice = item.price.ToString() + " ש\"ח";
                if (!isOrange)
                {
                    sAppName = item.name.Insert(0, "*");
                    sPrice = "באתר הספק";
                }
                else
                {
                    if (item.ismonthly == 1)
                    {
                        sPrice = "תשלום חודשי";
                    }
                }

                string sAltText = item.name;
                StringBuilder sb = new StringBuilder();

				sb.Append("<li class=\"clearfix\">");
				sb.AppendFormat("<div class=\"thumb\"><a href=\"AppDetails.aspx?aid={0}\"><img src=\"{1}\" alt=\"\"></a></div>", item.id, item.icon);
				sb.AppendFormat("<p class=\"text title\">{0}</p>", item.name);
				sb.AppendFormat("<p class=\"text details\">{0}</p>", item.shortdescription);
				sb.AppendFormat("<p class=\"text price\">{0}₪</p>", item.price);
				sb.AppendFormat("<div class=\"like\">487</div>", item.shortdescription);
				sb.AppendFormat("<a href=\"\" class=\"button button-orange\">פרק ראשון חינם</a>");
				sb.Append("<div class=\"cut\"><span class=\"shadow\"></span><span class=\"highlight\"></span></div><!-- cut --></li>");

                sAppsList += sb.ToString();
            }
        }

        private void GetParameters()
        {
            iCatID = Convert.ToInt32(Request.QueryString["id"]);
            isTop = Convert.ToBoolean(Convert.ToInt32(Request.QueryString["Top"]));
            iCurrentPage = Convert.ToInt32(Request.QueryString["page"]);
            iPageSize = Convert.ToInt32(Request.QueryString["pagesize"]);
            iStartNum = ((iCurrentPage > 0) ? iCurrentPage : 0) * iPageSize;
            _pageType = (PageType)Convert.ToInt32(Request.QueryString["type"]);
            _SearchTerm = (Request.QueryString["str"] != null) ? Request.QueryString["str"].ToString() : string.Empty;
        }
    }
}
